package com.aispeech.task.common.data.domain;

import com.aispeech.task.common.data.constant.Progress;
import com.aispeech.task.common.data.enums.APICode;
import lombok.ToString;

import java.util.*;

@ToString
public final class Task {
    int progress;
    List<Map> result;
    private String id;
    private String productId;
    private int priority = 0;
    private String hostName;

    public boolean isFinished() {
        return finished;
    }

    public void setFinished(boolean finished) {
        this.finished = finished;
    }

    private long tsUpdate;
    private boolean finished;
    private Map request;
    private Map metrics;
    private Map error;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public Task() {
        metrics = new HashMap(){{ put("ts_received", System.currentTimeMillis()); }};
        result = new ArrayList<>();

        finished = false;
    }

    public static Task of(String productId, Map<String, Object> request) {
        Task task = new Task();

        task.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        task.setProductId(productId);
        task.setProgress(Progress.IDLE);
        task.setRequest(request);

        return task;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    public List<Map> getResult() {
        return result;
    }

    public void setResult(List<Map> result) {
        this.result = result;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public long getTsUpdate() {
        return tsUpdate;
    }

    public void setTsUpdate(long tsUpdate) {
        this.tsUpdate = tsUpdate;
    }

    public Map getRequest() {
        return request;
    }

    public void setRequest(Map request) {
        this.request = request;
    }

    public Map getMetrics() {
        return metrics;
    }

    public void setMetrics(Map metrics) {
        this.metrics = metrics;
    }

    public Map getError() {
        return error;
    }

    public void setError(Map error) {
        this.error = error;
    }

    public void updateError(APICode code) {
        updateError(code.getCode(), code.getMessage());
    }

    public void updateError(int code, String message) {
        if (error == null)
            error = new HashMap();

        error.put("code", code);
        error.put("message", message);
    }
}
